// Important.. otherwise it's possible it will execute on multiple systems :)
if (!isServer) exitWith {};

// Those variables are mandatory, they are used in the mission interface to describe the mission.
DM_Mission_Name="STR_MF_M_MANHUNT_NAME";
DM_Mission_Desc="STR_MF_M_MANHUNT_DESC";
DM_Mission_Keywords="Day, Assault, Dynamic";
DM_Mission_Chapters = 1;
DM_Mission_FolderName = "mission9";
DM_Mission_StartTime = [12,0]; // In the format [hours, minutes];
DM_Mission_StartRain = 0.1; // 0 = no rain, 1 = max rain
DM_Mission_StartOvercast = 0.5; // 0 = no clouds,  1 = stormy

// Variables internal to the mission:
DM_Mission_Chapter_Success = 0; // 0 is pending, 1 = success, -1 = failure. You can use other values in chapters for multi-branching.
DM_Mission_Mission_CanRun = True; // Setting it to false will exit the chapters and mission execution. This allows for interrupting the mission.
DM_Mission_Mission_Runs = False; // This will be set to false when the mission is stopped
DM_Mission_Chapter_CanRun = True; // Setting it to false will exit the chapters and mission execution. This allows for interrupting the mission.
DM_Mission_Chapter_Runs = False; // The current status of the running chapter

// This function will manage the mission itself. It will create the necessary objects and manage them.
// When this function exits, the XfMissionSuccess/Failure will be loaded and finally the cleanup.
// In the case of this example mission, we will make use of chapters to manage the different parts of the mission.
// The advantage of doing so is that we can have branching chapters which lead to different possibilities/endings or even a random selection of chapters (to give more variety)
XfMissionExecute={
	private ["_currentChapterFile","_currentChapterIndex","_chapters_successes"];
	diag_log "";
	diag_log "";
	diag_log "";
	diag_log "=========================================================================";
	diag_log "=========================================================================";
	diag_log "                                                                         ";
	diag_log Format["    Mission : %1   ",DM_Mission_Name];
	diag_log "                                                                         ";
	diag_log "=========================================================================";
	diag_log "=========================================================================";
	diag_log "";
	DM_Mission_Mission_CanRun = True;
	DM_Mission_Mission_Runs = True;
	DM_Mission_Chapter_CanRun = True;
		
	diag_log Format["(%1)[%2]: %3: %4",diag_tickTime,DM_Mission_FolderName,"Starting execution!"];
	// Setting up parameters internal to the mission if they are overridden
	_this call XfMissionParseParameters;
	
	// Calling the function which will create all necessary initializations, units, weather conditions, etc.
	call XfMissionSetup;
	sleep 5;

	// We will manage the mission body through chapters files, it's not required, but it's cleaner and make the mission file lighter (important when loading the mission list).
	// This also allows for better seeing the mission structure if you want to do branchings and so on.
	// In this example mission there will be no branching, only a straight follow up of objectives. Other demo missions will contain more complex structures.	
	
	// This value will add up the success and failures to give a final "score"
	_chapters_successes = 0;
	// We loop through all chapters of the mission.
	For [{_currentChapterIndex = 1},{_currentChapterIndex <= DM_Mission_Chapters},{_currentChapterIndex = _currentChapterIndex + 1}] do {
		if (!DM_Mission_Mission_CanRun) exitWith {["d_msg",[Format["The current mission (%1) has been canceled!",DM_Mission_Name],"all",objNull,"global"]] call XNetCallEvent;};
		// We initialize the variable to start cleanly :)
		DM_Mission_Chapter_Success = 0;
		// Then we compute the chapter filename.
		_currentChapterFile = Format["missions\m\%1\chapter%2.sqf",DM_Mission_FolderName,_currentChapterIndex];
		diag_log Format["(%1)[%2]: %3 %4: %5",diag_tickTime,DM_Mission_FolderName,"Compiling Chapter",_currentChapterIndex,_currentChapterFile];
		// This variable is used in all chapters files, this is set to true at the end of the chapter file, so we know if it compiled correctly.
		DM_Mission_Chapter_Compiled = false;
		// We compile it (or at least try to)
		call compile preprocessFileLineNumbers _currentChapterFile;
		// We check if the file did compile.
		if (DM_Mission_Chapter_Compiled) then {
			diag_log Format["(%1)[%2]: %3 %4",diag_tickTime,DM_Mission_FolderName,"Starting Chapter", _currentChapterIndex];
			// We spawn the Execution of the chapter.
			[] spawn XfMissionChapterExecute;
			// We wait for the Chapter to end executing, either by a success of by a failure.
			waituntil {sleep 10; (!DM_Mission_Chapter_Runs)};
			// We add up the chapters successes and failures to obtain a final outcome.
			_chapters_successes = _chapters_successes + DM_Mission_Chapter_Success;
		};
		If (!DM_Mission_Chapter_Compiled) exitWith{
			["STR_MF_COMPILEFAILED",_currentChapterIndex] call XfMissionMessageToAll;
			diag_log Format["(%1)[%2]: %3 %4 failed",diag_tickTime,DM_Mission_FolderName,"Compiling Chapter",_currentChapterIndex];
		};
	};
	If (DM_Mission_Mission_CanRun) then {
		// We evaluate the success level of the mission.
		if (DM_Mission_Chapter_Success > 0) then {
			call XfMissionSuccess;
		} else {
			call XfMissionFailure;
		};
		sleep 20;
	};
	call XfMissionCleanUp;
	DM_Mission_Mission_Runs = false;
};

// This function will create the initial setup of the mission like a briefing, notes, objects, markers etc.
XfMissionSetup={
	private ["_mission_date"];
	diag_log Format["(%1)[%2]: %3",diag_tickTime,DM_Mission_FolderName,"Starting Setup!"];
	_mission_date = DM_Mission_StartTime call XfMissionGetDate;
	[DM_Mission_Name,_mission_date] call XfMissionStartScreen;
	// Changes the weather to the given values over a period of time of 60 seconds.
	[60,DM_Mission_StartRain,DM_Mission_StartOvercast] call XfMissionSetWeather;

	diag_log Format["(%1)[%2]: %3",diag_tickTime,DM_Mission_FolderName,"Ending Setup!"];
};

XfMissionSuccess={
	diag_log Format["(%1)[%2]: %3",diag_tickTime,DM_Mission_FolderName,"Starting Success!"];
};

XfMissionFailure={
	diag_log Format["(%1)[%2]: %3",diag_tickTime,DM_Mission_FolderName,"Starting Failure!"];
};

// Self-explanatory
XfMissionCleanUp={
	diag_log Format["(%1)[%2]: %3: %4",diag_tickTime,DM_Mission_FolderName,"Starting CleanUp!"];
	[] call XfMissionCleanAll;
	[] call XfMissionCloseMission;
};

// Obvious I'd say...
XfMissionCancel = {
	DM_Mission_Mission_CanRun = false;
	DM_Mission_Chapter_CanRun = false;
	sleep 10;
	DM_Mission_Chapter_Runs = false;
};
